package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.SharePhotoBean;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: SharePhotoAdapter
 * @Description: 共享秀照adapter
 * @Author: ljp
 * @CreateDate: 2019/1/15 19:28
 * @Version: 1.0
 */
public class SharePhotoAdapter extends BaseAdapter<SharePhotoBean.PageBean.ListBean> {
    private SetItemClick setItemClick;
    private List<String> sb;
    private String TAG = "SharePhotoAdapter";
    private String flag;

    public void setSetItemClick(SetItemClick setItemClick) {
        this.setItemClick = setItemClick;
    }

    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public SharePhotoAdapter(Context context, List<SharePhotoBean.PageBean.ListBean> tlist, int mLayout,String flag) {
        super(context, tlist, mLayout);
        sb = new ArrayList<>();
        this.flag = flag;
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ItemViewHolder(view);
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if(viewHolder instanceof ItemViewHolder){
            SharePhotoBean.PageBean.ListBean listBean = tlist.get(position);
            ItemViewHolder itemViewHolder = (ItemViewHolder) viewHolder;
            Log.i(TAG, "onBindViewHolder: ----v-----+"+listBean.isSelectDelete());
            if(flag.equals("me")){
                itemViewHolder.imagePayTag.setVisibility(View.GONE);
                if(listBean.getStatus() == 0){//0审核中 1审核通过 2驳回
                    itemViewHolder.relaAuditStatus.setVisibility(View.VISIBLE);
                    itemViewHolder.tvAudit.setText("审核中");
                }else if(listBean.getStatus() == 2){
                    itemViewHolder.relaAuditStatus.setVisibility(View.VISIBLE);
                    itemViewHolder.tvAudit.setText("未通过");
                }else {
                    itemViewHolder.relaAuditStatus.setVisibility(View.GONE);
                }
            }

            if(!TextUtils.isEmpty(listBean.getCoverUrl())) {
                if (!listBean.getCoverUrl().equals(itemViewHolder.imageCover.getTag(R.id.image_cover))) {
                    // 加载图片
                    itemViewHolder.imageCover.setTag(R.id.image_cover, listBean.getCoverUrl());
                    ViewGroup.LayoutParams layoutParams = itemViewHolder.imageCover.getLayoutParams();
                    layoutParams.width = Utils.getSrecchWidth(context)/2;
                    itemViewHolder.imageCover.setLayoutParams(layoutParams);
                    GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP + listBean.getCoverUrl(),
                            itemViewHolder.imageCover);
                    if(listBean.isSelectDelete() == 0 ){//是删除还是选定 -1选定  1删除 0取消选定
                        itemViewHolder.imageSelect.setVisibility(View.GONE);
                    }else if(listBean.isSelectDelete() == -1){
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_un_delete);
                    }else {
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_select_delete);
                    }
                }else {
                    if(listBean.isSelectDelete() == 0 ){//是删除还是选定 -1选定  1删除 0取消选定
                        itemViewHolder.imageSelect.setVisibility(View.GONE);
                    }else if(listBean.isSelectDelete() == -1){
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_un_delete);
                    }else {
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_select_delete);
                    }
                }
            }
            if (listBean.getvType()== 1) {//判断是vip等级 0非会员 1月会员 2年会员
                itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                itemViewHolder.imageVip.setImageResource(R.mipmap.vip_month);
                itemViewHolder.tvName.setTextColor(context.getResources().getColor(R.color.colorFF3B5E));
            } else if (listBean.getvType()==2 ) {
                itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                itemViewHolder.imageVip.setImageResource(R.mipmap.vip_year);
                itemViewHolder.tvName.setTextColor(context.getResources().getColor(R.color.colorFF3B5E));
            } else {
                itemViewHolder.imageVip.setVisibility(View.GONE);
                itemViewHolder.tvName.setTextColor(context.getResources().getColor(R.color.color333333));
            }
            itemViewHolder.tvName.setText(listBean.getNickname());
            if (listBean.getGender() == 1) {//判断性别  1男 2女
                itemViewHolder.imageSex.setImageResource(R.mipmap.boy);
                itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_blue);
            } else if (listBean.getGender() == 2) {
                itemViewHolder.imageSex.setImageResource(R.mipmap.girl);
                itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_red);
            }
            if (!TextUtils.isEmpty(listBean.getBirthday())) {
                String substring = listBean.getBirthday().substring(0, 4);
                String time = Utils.getStringTime(new Date());
                int age = (Integer.parseInt(time)) - (Integer.parseInt(substring));
                itemViewHolder.tvAge.setText(age + "");
            }
            if(listBean.getSign() == 1){//付金币标记 0未付 1已付
                itemViewHolder.imagePayTag.setImageResource(R.mipmap.share_photo_open);
            }else {
                itemViewHolder.imagePayTag.setImageResource(R.mipmap.share_photo_off);
            }
            itemViewHolder.tvTitle.setText(listBean.getTitle());
            itemViewHolder.tvGoldParse.setText(listBean.getGold()+"金币");
            itemViewHolder.tvPhotoLookNum.setText(listBean.getNumber()+"人已浏览");
            String[] strings = Utils.spitPictureUrl(listBean.getImgUrl());
            itemViewHolder.tvPhotoNum.setText(strings.length+"");
            itemViewHolder.imageCover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listBean.isSelectDelete() == 0 ){//是删除还是选定 -1选定  1删除 0取消选定
                        itemViewHolder.imageSelect.setVisibility(View.GONE);
                        setItemClick.setLookPhotoItem(position,listBean);
                    }else if(listBean.isSelectDelete() == -1){
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_select_delete);
                        listBean.setSelectDelete(1);
                        sb.add(listBean.getId());
                    }else {
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_un_delete);
                        listBean.setSelectDelete(-1);
                        if(sb.contains(listBean.getId())){
                            sb.remove(listBean.getId());
                        }
                    }

                }
            });

        }

    }
    public List<String> getSb() {
        return sb;
    }
    class ItemViewHolder extends BaseViewHolder{
        @BindView(R.id.image_cover)
        CircleImageView imageCover;
        @BindView(R.id.tv_photo_num)
        TextView tvPhotoNum;
        @BindView(R.id.tv_photo_look_num)
        TextView tvPhotoLookNum;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_gold_parse)
        TextView tvGoldParse;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.image_sex)
        ImageView imageSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.linear_sex_age)
        LinearLayout linearSexAge;
        @BindView(R.id.image_vip)
        ImageView imageVip;
        @BindView(R.id.image_pay_tag)
        ImageView imagePayTag;
        @BindView(R.id.image_un_select)
        ImageView imageSelect;
        //审核状态
        @BindView(R.id.rela_auditStatus)
        RelativeLayout relaAuditStatus;
        @BindView(R.id.tv_auditStatus)
        TextView tvAudit;
        View view;
        public ItemViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            ButterKnife.bind(this, itemView);
        }
    }
    public interface SetItemClick{
        void setLookPhotoItem(int position,SharePhotoBean.PageBean.ListBean listBean);
    }

}
